<template>
  <div>
    <div class="header" style="padding-left: 25px;">
      <span style="line-height: 40px; color: #eef1f4"><b>{{ order.create_time }}</b></span>
      <span style="line-height: 40px; color: #eef1f4; margin-left: 30px"><b>订单编号： {{ order.order_no }}</b></span>
    </div>
    <div class="body">
      <div style="display: inline;line-height: 40px">
        <table>
          <tr>
            <th>商品图片</th>
            <th>商品</th>
            <th>规格</th>
            <th>数量</th>
            <th>总价</th>
            <th>收货人</th>
            <th>订单状态</th>
          </tr>
          <tr>
            <td>
              <span>
                <router-link :to="'goodview/'+order.good_id">
                  <img :src="baseApi + order.imgs" style="width: 100px;height:100px">
                </router-link>
              </span>
            </td>
            <a :href="'goodview/'+order.good_id">
              <td>{{ order.good_name }}</td>
            </a>
            <td>{{ order.standard }}</td>
            <td>{{ order.count }}</td>
            <td>{{ order.total_price }}</td>
            <el-popover
                placement="bottom-start"
                width="200"
                trigger="hover"
                :content=address>
              <td slot="reference" style="color: #42b983">{{ order.link_user }}</td>
            </el-popover>
            <!--          订单状态-->
            <template v-if="order.state==='已发货'">
              <td style="color: #42b983">{{ order.state }}</td>
              <td>
                <el-button style="margin-left: 20px;font-size: 15px;" type="primary" @click="receive">确认收货
                </el-button>
              </td>
            </template>

            <template v-else-if="order.state==='已收货'">
              <td style="color: #42b983"><a class="el-icon-check"></a>{{ order.state }}</td>
            </template>

            <template v-else-if="order.state==='已支付'">
              <td style="color: #3b62f8"> {{ order.state }}</td>
              <td>
                <el-button style="font-size: 15px;" type="info" plain disabled>等待发货</el-button>
              </td>
            </template>

            <template v-else>
              <td>{{ order.state }}</td>
              <td>
                <el-button style="margin-left: 20px;font-size: 15px;" type="success" @click="pay">去支付</el-button>
              </td>
            </template>
            <td>
              <el-button type="primary" v-if="order.state==='已收货'" @click="dialogVisible = true">我要评价</el-button>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <!--  评价商品弹窗-->
    <el-dialog
        title="评价商品"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">

      <div class="block">
        <span>评分</span>
        <el-rate
            v-model="value2"
            :colors="colors">
        </el-rate>
        <span>评价</span>
        <el-input type="textarea" v-model="review"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitReview()">提交</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "OrderItem",
  props: {
    order: Object,
  },
  created() {
    console.log(this.order)
  },
  data() {
    return {
      address: '电话:' + this.order.link_phone + ' 地址:' + this.order.link_address,
      baseApi: this.$store.state.baseApi,
      dialogVisible: false,
      value2: null,
      review: '',
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
    }
  },
  methods: {
    //跳转到支付页面
    pay() {
      this.$router.push({path: 'pay', query: {money: this.order.total_price, orderNo: this.order.order_no}})
    },
    //确认收货
    receive() {

      this.$confirm('是否确认收货?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {

        this.request.get("/api/order/received/" + this.order.order_no).then(res => {
          if (res.code === '200') {
            this.$message.success("收货成功");
            this.order.state = '已收货'
          }
        })
      })

    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    submitReview() {
      this.request.post("/api/review", {
        productId: this.order.good_id,
        rating: this.value2,
        reviewText: this.review,
      }).then(res => {
        if (res.code === '200') {
          this.$message.success("评价成功");
          this.dialogVisible = false;
        }
      });
      this.dialogVisible = false;
    }
  }
}
</script>

<style scoped>
.header {
  background-color: #fa3d4a;
  height: 40px;
  width: 96%;
//border-radius: 25px 25px 0 0;
}

.body {
  background-color: white;
  padding: 20px;
  width: 95%;
//border-radius: 0 0 25px 25px; margin-bottom: 10px;
}

th, td {

  width: 120px;
  text-align: center;
}

th {
  font-size: 15px;
  color: #fa3d4a;
  font-weight: normal;
}
</style>